package cn.wen.mybatis.framework.executor.statement;

import java.sql.SQLException;
import java.sql.Statement;


public class StatementUtil {

  private StatementUtil() {
    // NOP
  }


  /**
   * 判断事务是否超时
   * @param statement
   * @param queryTimeout
   * @param transactionTimeout
   * @throws SQLException
   */
  public static void applyTransactionTimeout(Statement statement, Integer queryTimeout, Integer transactionTimeout) throws SQLException {
    if (transactionTimeout == null){
      return;
    }
    Integer timeToLiveOfQuery = null;
    if (queryTimeout == null || queryTimeout == 0) {
      timeToLiveOfQuery = transactionTimeout;
    } else if (transactionTimeout < queryTimeout) {
      timeToLiveOfQuery = transactionTimeout;
    }
    if (timeToLiveOfQuery != null) {
      statement.setQueryTimeout(timeToLiveOfQuery);
    }
  }

}
